let s:MSG = api#import('message')
" line: 3,
" col: 80,
let s:MSG.col = 10
" minwidth: 120,
" time: 3000,
" tabpage: -1,
" zindex: 300,
" drag: 1,
" highlight: 'String',
" border: [],
" close: 'click',
" padding: [0,1,0,1],

function! plugin#vim_fugitive#print(text) abort
    let lines = split(a:text, '\n')
    return s:MSG.print(lines)
endfunction

function! plugin#vim_fugitive#init() abort
    nnoremap <m-g>  :call plugin#vim_fugitive#next_action()<cr>
    nnoremap <F4>   :call plugin#vim_fugitive#print(plugin#vim_fugitive#status())<cr>
    nnoremap <F3>   :call plugin#vim_fugitive#print(execute('Git remote -v'))<cr>
endfunction

function! plugin#vim_fugitive#status() abort
    return execute('Git status', "silent!")
endfunction
" \|no changes added to commit (use \"git add\" and/or \"git commit -a\")
let g:git_expressions = {
            \ 'init': "nothing to commit, but untracked files present",
            \ 'stage': "Changes not staged for commit", 
            \ 'commit': "Changes to be committed", 
            \ 'push': "use \"git push\" to publish",
            \ 'clean': "nothing to commit, working tree clean",
            \ }

function! plugin#vim_fugitive#next_action() abort
    Gcd 
    let status = plugin#vim_fugitive#status()
    let clean = !empty(matchstr(status, g:git_expressions.clean))
    let stage = !empty(matchstr(status, g:git_expressions.stage))
    let commit = !empty(matchstr(status, g:git_expressions.commit))
    let push = !empty(matchstr(status, g:git_expressions.push))
    let header = "clean: " . clean . "\tstage: " . stage . "\t commit: " . commit . "\t push: " . push
    if stage
        return plugin#vim_fugitive#stage() 
    endif 
    if commit
        return plugin#vim_fugitive#commit()
    endif
    if push
        return plugin#vim_fugitive#push()
    endif
    if clean
        return plugin#vim_fugitive#print(status)
    else
        echohl Error
        echomsg status
        echohl None
    endif
endfunction

function! plugin#vim_fugitive#modified() abort
    return !empty(matchstr(plugin#vim_fugitive#status(), "use \"git add\"|Changes not staged for commit"))
endfunction

function! plugin#vim_fugitive#stage() abort
    Git add .
    " return s:MSG.print(split(plugin#vim_fugitive#status(), '\n'))
endfunction

function! plugin#vim_fugitive#staged() abort
    return !empty(matchstr(plugin#vim_fugitive#status(), "Changes to be committed"))
endfunction

function! plugin#vim_fugitive#commit() abort
    Git commit
    " return s:MSG.print(split(plugin#vim_fugitive#status(), '\n'))
endfunction

function! plugin#vim_fugitive#to_push() abort
    return !empty(matchstr(plugin#vim_fugitive#status(), "use \"git push\" to publish"))
endfunction

function! plugin#vim_fugitive#push() abort
    call s:MSG.print("Waiting for push")
    Git push
    " return s:MSG.print(split(plugin#vim_fugitive#status(), '\n'))
endfunction

function! plugin#vim_fugitive#load() abort
    packadd asyncrun.vim
    packadd vim-fugitive
endfunction


